#!/bin/bash

# -------------------------------------------
# Checks current Open AT environment settings
# -------------------------------------------

# Check output file if required
if [ ! -z "$1" ] && [ "$1" != -n ]
then
    # Create new file
    rm -f $1
    touch $1
    
    # Create log file command
    LogFile="$1"
else
    # No log file
    LogFile="/dev/null"
fi

# Check compiler
if [ ! -z "$WMCOMPILER" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display compiler
if [ "$1" != -n ]; then echo "Current target compiler: $WMCOMPILER $Result" | tee -a $LogFile
fi;

# Check memory
if [ ! -z "$WMMEMORY" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display memory
if [ "$1" != -n ]; then echo "Current target memory size: $WMMEMORY $Result" | tee -a $LogFile
fi;

# Check CoreSw
if [ ! -z "$WMCORESW" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display CoreSw
if [ "$1" != -n ]; then echo "Current target Core Software: $WMCORESW $Result" | tee -a $LogFile
fi;

# Check synchro list
if [ ! -z "$WMSYNCLIST" ]
then
    Result="[OK]"
else
    Result="[unset]"
fi

# Display synchro list
if [ "$1" != -n ]; then echo "Current IDE workspaces synchro list: $WMSYNCLIST $Result" | tee -a $LogFile
fi;

# Check SGT_DIR
if [ ! -z "$SGT_DIR" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display SGT_DIR
if [ "$1" != -n ]; then echo "Current SGT directory: $SGT_DIR $Result" | tee -a $LogFile
fi;

# Check SGT_VER
if [ ! -z "$SGT_VER" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display SGT_VER
if [ "$1" != -n ]; then echo "Current SGT version: $SGT_VER $Result" | tee -a $LogFile
fi;

# Check SGT_SCRIPT
if [ ! -z "$SGT_SCRIPT" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display SGT_SCRIPT
if [ "$1" != -n ]; then echo "Current SGT scripts directory: $SGT_SCRIPT $Result" | tee -a $LogFile
fi;

# Check make_lib
Tmp=$(which make_lib 2> /dev/null)
if [ ! -z "$Tmp" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display make_lib
if [ "$1" != -n ]; then echo "Check make_lib script: $Tmp $Result" | tee -a $LogFile
fi;

# Check WMATHOME
if [ ! -z "$WMATHOME" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display WMATHOME
if [ "$1" != -n ]; then echo "Current Open AT directory: $WMATHOME $Result" | tee -a $LogFile
fi;

# Check WMATHOME_DOS
if [ ! -z "$WMATHOME_DOS" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display WMATHOME_DOS
if [ "$1" != -n ]; then echo "Current Open AT directory (for Windows): \"$WMATHOME_DOS\" $Result" | tee -a $LogFile
fi;

# Check WMGCCHOME
if [ ! -z "$WMGCCHOME" ]
then
    Result="[OK]"
else
    Result="[unset]"
fi

# Display WMGCCHOME
if [ "$1" != -n ]; then echo "Current GCC compiler directory: $WMGCCHOME $Result" | tee -a $LogFile
fi;

# Check ARMDLL
if [ ! -z "$ARMDLL" ]
then
    Result="[OK]"
else
    Result="[unset]"
fi

# Display ARMDLL
if [ "$1" != -n ]; then echo "Current ARM compiler directory: $ARMDLL $Result" | tee -a $LogFile
fi;

# Check WMLIBHOME
if [ ! -z "$WMLIBHOME" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display WMLIBHOME
if [ "$1" != -n ]; then echo "Current Add-on libraries directory: $WMLIBHOME $Result" | tee -a $LogFile
fi;

# Check WMTOOLS
if [ ! -z "$WMTOOLS" ]
then
    Result="[OK]"
else
    Result="[unset]"
fi

# Display WMTOOLS
if [ "$1" != -n ]; then echo "Current Tools base directory: $WMTOOLS $Result" | tee -a $LogFile
fi;

# Check WMCOREHOME
if [ ! -z "$WMCOREHOME" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display WMCOREHOME
if [ "$1" != -n ]; then echo "Current Core Software root directory: $WMCOREHOME $Result" | tee -a $LogFile
fi;

# Check valid Core SW directory
if [ -f "$WMCOREHOME/$WMCORESW/backtraces64k.axf" ] || [ -f "$WMCOREHOME/$WMCORESW/out/tmt/backtraces64k.axf" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display Core software directory check result
if [ "$1" != -n ]; then echo "Check Core Software root directory consistency: $WMCOREHOME/$WMCORESW $Result" | tee -a $LogFile
fi;

# Check wmnew
Tmp=$(which wmnew 2> /dev/null)
if [ ! -z "$Tmp" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display wmnew
if [ "$1" != -n ]; then echo "Check wmnew script: $Tmp $Result" | tee -a $LogFile
fi;

# Check wmmake
Tmp=$(which wmmake 2> /dev/null)
if [ ! -z "$Tmp" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display wmmake
if [ "$1" != -n ]; then echo "Check wmmake script: $Tmp $Result" | tee -a $LogFile
fi;

# Check wmsync
Tmp=$(which wmsync 2> /dev/null)
if [ ! -z "$Tmp" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display wmsync
if [ "$1" != -n ]; then echo "Check wmsync script: $Tmp $Result" | tee -a $LogFile
fi;

# Check wmsetup
Tmp=$(which wmsetup 2> /dev/null)
if [ ! -z "$Tmp" ]
then
    Result="[OK]"
else
    Error=1
    Result="[ERROR]"
fi

# Display wmsetup
if [ "$1" != -n ]; then echo "Check wmsetup script: $Tmp $Result" | tee -a $LogFile
fi;



# End
if [ "$Error" == 1 ]
then
    echo
    echo The Open AT environment is not correctly installed.
    echo Please run the \$WMATHOME/TgtGen/WmGen/wmsetup script or refer to the Tools Manual.
    exit 1
fi
